module utils {
    export class HtmlUtil {
        /*
           字符串转换成html格式，返回textFlow的值Array<egret.ITextElement>
           #H 指令结束
           #B 加粗  #B你好#H
           #I 倾斜  #你好#H
           #C 颜色  #C(0xfbfe00)你好#H
           #S 字体大小 #S(12)你好#H
           #K  描边大小 #K4)你好#H
           #KC  描边颜色 #KC(0xfbfe00)你好#H
         */
        static parseStrToHtml(str): Array<egret.ITextElement> {
            //str = "#B#I#C(0x00ff00)#S(32)你好#H#H#H#H,nice to #stroke(4)#strokeColor(0x008822)#I#C(0x000000)meet#H#H#H#H you #H";
            var htmlArr: Array<egret.ITextElement> = [];
            if (str) {
                var reg: RegExp = /(#I|#B|#C\(0x[0-9a-fA-F]{1,6}\)|#S\(\d{1,2}\)|#K\(\d{1,2}\)|#KC\(0x[0-9a-fA-F]{1,6}\)|#A\(\)|#H|#U)/g;
                var strArr = str.split(reg);
                if (strArr) {
                    var gmNum = 0;
                    var htmlText: string = "";
                    var obj: any = {};
                    while (strArr.length) {
                        var str = strArr.shift();
                        var index = str.indexOf("(");
                        var gmStr = (index != -1) ? str.substring(0, index) : str;
                        switch (gmStr) {
                            case "#I":
                                if (!obj.style) { obj.style = {} };
                                obj.style["italic"] = true;
                                gmNum++;
                                break;
                            case "#B":
                                if (!obj.style) { obj.style = {} };
                                obj.style["bold"] = true;
                                gmNum++;
                                break;
                            case "#C":
                                if (!obj.style) { obj.style = {} };
                                obj.style["textColor"] = Number(str.match(/0x[0-9a-fA-F]{1,6}/)[0]);
                                gmNum++;
                                break;
                            case "#S":
                                if (!obj.style) { obj.style = {} };
                                obj.style["size"] = str.match(/\d{1,2}/)[0];
                                gmNum++;
                                break;
                            case "#K":
                                if (!obj.style) { obj.style = {} };
                                obj.style["stroke"] = str.match(/\d{1,2}/)[0];
                                gmNum++;
                                break;
                            case "#KC":
                                if (!obj.style) { obj.style = {} };
                                obj.style["strokeColor"] = Number(str.match(/0x[0-9a-fA-F]{1,6}/)[0]);
                                gmNum++;
                                break;
                            case "#H":
                                gmNum--;
                                break;
                            case "#U":
                                if (!obj.style) { obj.style = {} };
                                obj.style["underline"] = true;
                                gmNum++;
                                break;
                            default:
                                htmlText += str;
                                break;
                        }
                        if ((gmNum == 0 || strArr.length == 0) && htmlText != "") {
                            obj.text = htmlText;
                            htmlArr.push(obj);
                            obj = {};
                            htmlText = "";
                        }
                    }
                }
            }
            return htmlArr;
        }
    }
}
